package com.wzz.seckillone.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wzz.seckillone.mapper.UserMapper;
import com.wzz.seckillone.pojo.User;
import com.wzz.seckillone.service.UserService;
import com.wzz.seckillone.utils.*;
import com.wzz.seckillone.common.AjaxResult;
import com.wzz.seckillone.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-10-28
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public User test(String id) {
        return userMapper.test(id);
    }

    @Override
    public AjaxResult doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String fromPass = loginVo.getPassword();
        AjaxResult result = new AjaxResult();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(fromPass)){
            return result.setCode(50010).setMessage("用户名或者密码isnull");
        }
        if(!ValidatorUtil.isMobile(mobile)){
            return result.setCode(50011).setMessage("手机号码格式出错");
        }
        User user = userMapper.getUserById(mobile);
        if(user == null){
            return result.setCode(50012).setMessage("无此用户");
        }
        if (!user.getPassword().equals(MD5Util.formPassToDbPass(fromPass,user.getSalt()))){
            System.out.println( );
            System.out.println(user.getPassword());
            System.out.println(user);
            System.out.println(MD5Util.formPassToDbPass(fromPass,user.getSalt()));
            return result.setCode(50013).setMessage("密码错误");

        }
        String ticket = UUIDUtil.uuid();
        //request.getSession().setAttribute(ticket,user);
        redisTemplate.opsForValue().set("user:"+ticket,user);
        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return result.setCode(200).setMessage("登录成功");
    }

    /*根据ticket 查询redis中存储的user信息*/
    @Override
    public User getUserByTicket(String params) {
        return (User) redisTemplate.opsForValue().get(params);
    }

    @Override
    public void register(User user) {
        user.setPassword(MD5Util.inputPassToDbPass(user.getPassword(),"1a2b3c4d"));
        if (user.getNickname() == null){
            user.setNickname("user");
        }
        if (user.getHead() == null){
            user.setHead("head");
        }
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date  reg = null;
        try {
            reg = format.parse(format.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setSalt("1a2b3c4d");
        user.setRegisterDate(reg);
        user.setLastLoginDate(null);
        user.setLoginCount(0);
        userMapper.saveUser(user);
    }

    /**
     * @Method
     * @Author wzz
     * @Version  1.0
     * @Description根据mobile 查询用户
     * @param
     * @Return
     * @Exception
     * @Date 2022/3/17 22:40
     */
    @Override
    public User getUserById(String username) {
        return userMapper.getUserById(username);
    }

    @Override
    public void saveOrUpdateUser(User user) {
        if (!StringUtils.isEmpty(user.getId())){
            //保存
            String salt = MD5Util.getSalt();
            user.setSalt(salt);
            user.setPassword(MD5Util.md5(user.getPassword()+salt));
            user.setLoginCount(0);
            user.setRegisterDate(new Date());
            userMapper.saveUser(user);
        }else{
            //修改
        }
    }

}
